# -*- coding: utf-8 -*-
# @Time    : 2020/2/7 1:04
# @Author  : Weymouth
# @Email   : 261229090@qq.com
# @File    : decrator.py
# @Software: PyCharm
# ---------------------------------------------------

import time

# start = time.time()
# time.sleep(1)
# end = time.time()
# print(end-start)
#
# print(start)
#遵守开放封闭原则，原代码不可修改，可进行扩展
# def foo():
#     start = time.time()
#     print('foo')
#     time.sleep(2)
#     end = time.time()
#     print('spend %s' % (end-start))
# def bar():
#     print('bar......')
#     time.sleep(3)

# def show_time(f):
#     def inner(*x, **y):
#         start = time.time()
#         f(*x, **y)
#         end = time.time()
#         print('spend %s' % (end - start))
#     return inner

# @show_time  # foo = show_time(foo)
# def foo():
#     print('foo')
#     time.sleep(2)
#
# @show_time  # bar = show_time(bar)
# def bar():
#     print('bar......')
#     time.sleep(3)

#功能函数加参数
# @show_time
# def add(*a, **b):
#     sums = 0
#     for i in a:
#         sums+=i
#     print(sums)
#     time.sleep(1)

# foo = show_time(foo)
# bar = show_time(bar)
# foo()   #执行inner函数
# bar()
# add(1, 2, 3, 4)
# show_time(foo)
# show_time(bar)


#装饰器加参数
def logger(flag=''):
    def show_time(f):
        def inner(*x, **y):
            start = time.time()
            f(*x, **y)
            end = time.time()
            print('spend %s' % (end - start))
            if flag == 'abc':
                print('日志记录')
        return inner
    return show_time

@logger('abc')  #@show_time
def add(*a, **b):
    sums = 0
    for i in a:
        sums+=i
    print(sums)
    time.sleep(1)

@logger('true')
def bar():
    print('bar......')
    time.sleep(3)